<?php 
/**
* student入口
*/
class Student_home extends CI_Controller
{
    
    function __construct()
    {
        parent::__construct();
        //登陆状态检测
        if(!$this->session->userdata('r_user_in'))
            redirect('home');
        //权限检测
        if($this->session->userdata('r_role_id') != 3)
            redirect('home');
    }

    function index()
    {
        if(date_default_timezone_get() != "Asia/Shanghai") 
            date_default_timezone_set("Asia/Shanghai");
        //考试日期晚于这个日期的试卷可以查看
        $date_early_limit_check=date("Y-m-d",strtotime("-7 day"));
        //获取当前日期
        $today=date("Y-m-d");
        //准备数据              
        $this->load->model('user_model');
        $this->load->model('student_model');
        $this->load->model('major_model');
        $this->load->model('report_model');
        $this->load->model('module_model');
        $this->load->model('logic_class_model');
        $this->load->model('big_lecture_model');

        //获取用户信息
        $username=$this->session->userdata('r_username');
        $user=$this->user_model->get_by_username($username);
        $student=$this->student_model->get_by_user_id($user['id']);

        //获取学生参加的逻辑班
        $student_logic_class=$this->student_model->get_student_logic_class($student['id']);
        
        // echo "student_logic_class";
        // var_dump($student_logic_class);
        //获取所有逻辑班的信息
        $all_logic_class = array();
        foreach ($student_logic_class as $item) {
            $logic_class=$this->logic_class_model->get_by_id($item['logic_class_id']);
            array_push($all_logic_class, $logic_class);
        }

        // echo "all_logic_class";
        // var_dump($all_logic_class);

        //获取逻辑班下的考试模块
        $index = 0;
        $all_logic_class_module = array();
        foreach ($all_logic_class as $item) {
            $module_in_logic_class=$this->module_model->get_module_in_logic_class($item['big_lecture_id']);
            $big_lecture = $this->big_lecture_model->get_by_id($item['big_lecture_id']);
            //所有系统的模块和逻辑班信息叠加
            $module_add_logic_class = array();
            foreach ($module_in_logic_class as $item1) {

                //寻找每一个模块是否有可替换的定制模块
                $module_replace=$this->module_model->get_module_type_1_parent_teacher($item1['id'],$item['teacher_id']);
                // echo "module_replace";
                // var_dump($module_replace);

                //没有可替换的定制模块
                if (!$module_replace) {             
                    //在report表中查找是否有正在进行的考试                  
                    $report=$this->report_model->get_by_module_student_logic_class($item1['id'],$student['id'],$item['id']);
                    // var_dump($item1['id']);
                    // var_dump($student['id']);
                    // var_dump($item['id']);
                    //var_dump($report);
                    //没有考试
                    if (!$report) {
                        $each_module_add_logic_class = array(
                            'id' => $item1['id'], 
                            'name' => $item1['name'], 
                            'time_limit' => $item1['time_limit'], 
                            'type' => $item1['type'], 
                            'parent_id' => $item1['parent_id'], 
                            'teacher_id' => $item1['teacher_id'], 
                            'report_id' => '0',
                            'report_datetime' => '',
                            'report_grades' => '',
                            'report_state'=>2);
                    }
                    //已考试
                    else{
                        //检查当前的考试状态，根据时间判断，是否已经结束，还是可以继续进行
                        if($report['state']==0)
                        {
                            //检查考试时间
                            $now=date("Y-m-d H:i:s");
                            $this->load->helper('compare_time');
                            $time_past=compare_time_of_minute($report['datetime'],$now);
                            if($time_past<=$item1['time_limit'])
                            {
                                //如果还有时间继续考试
                            }
                            else
                            {
                                //如果已经超过时间，判卷，更新report信息
                                $this->refresh_grades($report['id']);
                                $report=$this->report_model->get_by_module_student_logic_class($item1['id'],$student['id'],$item['id']);
                            }
                        }
                        
                        
                        $report_date = date('Y-m-d',strtotime($report['datetime']));                        
                        if($report_date>=$date_early_limit_check)
                        {
                            $check_limit = 1;
                        }
                        else
                        {
                            $check_limit = 0;
                        }
                        
                        $each_module_add_logic_class = array(
                            'id' => $item1['id'], 
                            'name' => $item1['name'], 
                            'time_limit' => $item1['time_limit'], 
                            'type' => $item1['type'], 
                            'parent_id' => $item1['parent_id'], 
                            'teacher_id' => $item1['teacher_id'],
                            'report_id' => $report['id'],
                            'report_datetime' =>  $report['datetime'],
                            'report_grades' =>  $report['grades'],
                            'report_state'=>$report['state'],
                            'check_limit'=>$check_limit);
                    }
                    array_push($module_add_logic_class, $each_module_add_logic_class);
                }

                //有替换的模块
                else{
                    foreach ($module_replace as $item2) {
                        $report_replace=$this->report_model->get_by_module_student_logic_class($item2['id'],$student['id'],$item['id']);
                        //没有考试
                        if (!$report_replace) {
                            $each_module_add_logic_class_replace = array(
                                'id' => $item2['id'], 
                                'name' => $item2['name'], 
                                'time_limit' => $item2['time_limit'], 
                                'type' => $item2['type'], 
                                'parent_id' => $item2['parent_id'], 
                                'teacher_id' => $item2['teacher_id'],
                                'report_id' => '0',
                                'report_datetime' => '',
                                'report_grades' => '',
                                'report_state'=>2
                                );
                                

                        }
                        //已考试
                        else{
                            if($report_replace['state']==0)
                            {
                                //检查考试时间
                                $now=date("Y-m-d H:i:s");
                                $this->load->helper('compare_time');
                                $time_past=compare_time_of_minute($report_replace['datetime'],$now);
                                if($time_past<=$item1['time_limit'])
                                {
                                    //如果还有时间继续考试
                                }
                                else
                                {
                                    //如果已经超过时间，判卷，更新report信息
                                    $this->refresh_grades($report_replace['id']);
                                    $report_replace=$this->report_model->get_by_module_student_logic_class($item2['id'],$student['id'],$item['id']);
                                }
                            }

                            $report_date = date('Y-m-d',strtotime($report_replace['datetime']));
                            if($report_date>=$date_early_limit_check)
                            {
                                $check_limit = 1;
                            }
                            else
                            {
                                $check_limit = 0;
                            }

                            $each_module_add_logic_class_replace = array(
                                'id' => $item2['id'], 
                                'name' => $item2['name'], 
                                'time_limit' => $item2['time_limit'], 
                                'type' => $item2['type'], 
                                'parent_id' => $item2['parent_id'], 
                                'teacher_id' => $item2['teacher_id'],
                                'report_id' => $report_replace['id'],
                                'report_datetime' =>  $report_replace['datetime'],
                                'report_grades' =>  $report_replace['grades'],
                                'report_state'=>$report_replace['state'],
                                'check_limit'=>$check_limit
                            );
                        }
                        array_push($module_add_logic_class, $each_module_add_logic_class_replace);
                    }               
                    
                }
                
            }
            // echo "module_add_logic_class";
            // var_dump($module_add_logic_class);       
            $all_logic_class_module[$index] = array('id' => $item['id'],
                            'big_lecture_id' => $item['big_lecture_id'],
                            'logic_class_id' => $item['id'],
                            'logic_class_name' => $item['name'],
                            'logic_class_teacher_id' => $item['teacher_id'],
                            'logic_class_type' => $item['type'],
                            'big_lecture_name' => $big_lecture['name'],
                            'module_add_logic_class' => $module_add_logic_class);
            ++$index;
        }

        //var_dump($all_logic_class_module);
        if(md5($username)==$user['password'])
        {
            $change_password=0;
        }
        else
        {
            $change_password=1;
        }

        $major=$this->major_model->get_by_id($student['major_id']);
        
        $logic_classes_for_choose=$this->logic_class_model->get_logic_classes_for_choose($student['id'],$student['school_id'],$today);
        
        $data = array();
        $data['title']='学生';
        $data['change_password']=$change_password;
        $data['student']=$student;
        $data['major']=$major;
        $data['all_logic_class_module']=$all_logic_class_module;
        $data['logic_classes_for_choose']=$logic_classes_for_choose;
        
        $this->load->view('student_home/index',$data);

    }

    //选课
    function choose_logic_class($logic_class_id=0)
    {
        if($logic_class_id==0)
        {
            //表单验证
            //填写班级信息选课，在修改逻辑班为自然班后关闭了这个功能
            if($this->form_validation->run('student_home/choose_logic_class'))
            {
                $school_id=$this->session->userdata('r_school_id');
                $logic_class_number = $this->input->post('logic_class_number');
                $student_id = $this->input->post('student_id');
                $this->load->model('student_model');
                $student = $this->student_model->get_by_id($student_id);
                //验证逻辑班的学校和当前选课学生的学校是否相同
                if($school_id == $student['school_id'])
                {
                    $this->load->model('logic_class_model');
                    $this->load->model('teacher_model');
                    $this->load->model('big_lecture_model');
                    $this->load->model('school_model');
                    $this->load->model('module_model');
                    $logic_class = $this->logic_class_model->get_by_logic_class_number_and_school_id($logic_class_number,$school_id);
                    if(!$logic_class)
                    {
                        $this->session->set_flashdata('message','请核实授课班号');
                        redirect('student_home/index');
                    }
                    $teacher = $this->teacher_model->get_by_id($logic_class['teacher_id']);
                    $big_lecture = $this->big_lecture_model->get_by_id($logic_class['big_lecture_id']);
                    $school = $this->school_model->get_by_id($school_id);
                    $modules = $this->module_model->get_modules_by_big_lecture_id($logic_class['big_lecture_id']);
                    //var_dump($modules);
                    $data = array();
                    $data['title']='确认授课班信息';
                    $data['logic_class']=$logic_class;
                    $data['teacher']=$teacher;
                    $data['big_lecture']=$big_lecture;
                    $data['school']=$school;
                    $data['modules']=$modules;
                    $this->load->view('student_home/check_logic_class',$data);
                }
                else
                {
                    $this->session->set_flashdata('message','请核实授课班号');
                    redirect('student_home/index');
                }
            }
            else
            {
                $this->session->set_flashdata('message','请核实授课班号');
                redirect('student_home/index');
            }

        }
        else
        {
            //准备数据              
            $this->load->model('user_model');
            $this->load->model('student_model');
            $this->load->model('logic_class_model');
            $this->load->model('teacher_model');
            $this->load->model('big_lecture_model');
            $this->load->model('school_model');
            $this->load->model('module_model');   
            //获取用户信息
            $username=$this->session->userdata('r_username');
            $user=$this->user_model->get_by_username($username);
            $student=$this->student_model->get_by_user_id($user['id']);
            //获取学生参加的逻辑班
            $student_logic_class=$this->student_model->get_student_logic_class($student['id']);
            if($student_logic_class)
            {
                $this->session->set_flashdata('message','已有课程，禁止进入多个班级');
                redirect('student_home/index');
            }
            $school_id=$this->session->userdata('r_school_id');
            $logic_class = $this->logic_class_model->get_by_id($logic_class_id);
            if(!$logic_class)
            {
                $this->session->set_flashdata('message','请核实授课班号');
                redirect('student_home/index');
            }
            //验证逻辑班的学校和当前用户的学校是否相同
            if($school_id==$logic_class['school_id'])
            {
                $teacher = $this->teacher_model->get_by_id($logic_class['teacher_id']);
                $big_lecture = $this->big_lecture_model->get_by_id($logic_class['big_lecture_id']);
                $school = $this->school_model->get_by_id($school_id);
                $modules = $this->module_model->get_modules_by_big_lecture_id($logic_class['big_lecture_id']);
                //var_dump($modules);
                $data = array();
                $data['title']='确认班级信息';
                $data['logic_class']=$logic_class;
                $data['teacher']=$teacher;
                $data['big_lecture']=$big_lecture;
                $data['school']=$school;
                $data['modules']=$modules;
                $this->load->view('student_home/check_logic_class',$data);
            }
            else
            {
                $this->session->set_flashdata('message','请核实授课班号');
                redirect('student_home/index');
            }
        }
        
    }
    //确认选课
    function confirm_logic_class($logic_class_id)
    {
        //准备数据              
        $this->load->model('user_model');
        $this->load->model('student_model');
        $this->load->model('logic_class_model');
        //获取用户信息
        $username=$this->session->userdata('r_username');
        $user=$this->user_model->get_by_username($username);
        $student=$this->student_model->get_by_user_id($user['id']);
        //获取学生参加的逻辑班
        $student_logic_class=$this->student_model->get_student_logic_class($student['id']);
        if($student_logic_class)
        {
            $this->session->set_flashdata('message','已有课程，禁止进入多个班级');
            redirect('student_home/index');
        }
        $logic_class=$this->logic_class_model->get_by_id($logic_class_id);
        if($student['school_id']==$logic_class['school_id'])
        {
            $item = array(
                'student_id' => $student['id'],
                'logic_class_id' => $logic_class_id
                 );
            if($this->student_model->get_student_in_logic_class($student['id'],$logic_class_id))
            {
                $this->session->set_flashdata('message','您已经选择了这个授课班');
                redirect('student_home/index');
            }
            else
            {
                if($this->student_model->add_student_in_logic_class($item))
                {
                    $this->session->set_flashdata('message','选课成功');
                    redirect('student_home/index');
                }
                else
                {
                    $this->session->set_flashdata('message','选课失败');
                    redirect('student_home/index');
                }
            }
            
        }
        else
        {
            $this->session->set_flashdata('message','选课失败');
            redirect('student_home/index');
        }        
    }
    function delete_student_in_logic_class($logic_class_id,$student_id)
    {
        $this->load->model('student_model');
        if($this->student_model->get_student_in_logic_class($student_id,$logic_class_id))
        {
            if($this->student_model->student_delete_from_logic_class($logic_class_id,$student_id))
            {
                $this->session->set_flashdata('message','退课成功');
                redirect('student_home/index');
            }
            else
            {
                $this->session->set_flashdata('message','您已经进行了考试，不能退课');
                redirect('student_home/index');
            }
        }
        else
        {
            $this->session->set_flashdata('message','您的课程中没有这个授课班');
            redirect('student_home/index');
        }
    }

    /**
     * 更新一次考试的成绩，判分，内部函数，已经保证数据合法
     * @param  [type] $report_id      [description]
     * @return [type]                 [description]
     */
    function refresh_grades($report_id)
    {
        if(date_default_timezone_get() != "Asia/Shanghai") 
            date_default_timezone_set("Asia/Shanghai");
        //根据数据库存储的选择判分
        $this->load->model('report_model');
        $this->load->model('question_model');
        $questions_in_report=$this->report_model->get_from_question_in_report($report_id);
        $grades=0;
        foreach ($questions_in_report as $item) {
            $real_question = $this->question_model->get_by_id($item['question_id']);//考题
            if ($item['choose'] == $real_question['answer']) {
                $grades++;
            }   
        }

        //更新report状态
        //将结果存入report表
        $report_item = array(
               'grades' => (int)($grades*100.0/count($questions_in_report)),
               'state' => 1
            );
        $this->report_model->update($report_id,$report_item);
        
    }


    /**
     * 退出，删除session中的数据
     * @return [type]
     */
    function logout()
    {
        $this->session->sess_destroy();
        redirect('home');
    }   

    /**
     * 进入考试页面
     * @return [type]
     */
    function test()
    {
        $data = array();
        $data['title']='电路基本问题及分析方法';
        $this->load->view('student_home/test',$data);
    }   

    /**
     * 查看考试结果
     * @return [type] [description]
     */
    function check_detail()
    {
        $data['title']='查看考试结果';
        $this->load->view('student_home/check_detail',$data);
    }
    

    /**
     * 修改密码
     * @return [type] [description]
     */
    function change_password()
    {
        //表单验证
        if($this->form_validation->run('student_home/change_password'))
        {
            //post方法
            //读取数据
            $old_password=$this->input->post('old_password');
            $new_password=$this->input->post('new_password');
            
            $username=$this->session->userdata('r_username');
            $this->load->model('user_model');
            $user=$this->user_model->check_user_valid($username,$old_password);
            if($user)
            {
                $user['password']=md5($new_password);
                if($this->user_model->update($user['id'],$user))
                {
                    $this->session->set_flashdata('message','修改成功');
                    redirect('student_home/index');
                }
                else
                {
                    $this->session->set_flashdata('message','操作失败');
                    redirect('student_home/index');
                }
                    
            }
            else{
                $this->session->set_flashdata('message','密码错误，修改失败');
                redirect('student_home/index'); 
            }
                        
        }

        //get方法
        $data['title']='修改密码';
        $this->load->view('student_home/change_password',$data);
    }
}
 